package com.cqw.controller;
import com.alibaba.fastjson.JSON;
import com.cqw.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-07-05 10:32
 * @Description: redis测试
 */
@RestController
@RequestMapping("/redis")
public class RedisController {


    @Autowired
    private RedisTemplate redisTemplate;


    private final static String key = "redis:key";


    @GetMapping("/string")
    public String stringRedis() {
        SysUser user = new SysUser();
        user.setUserId(1l);
        user.setUserName("张三");
        user.setPassword("123456");
        redisTemplate.opsForValue().set(key, JSON.toJSONString(user));
        return "OK";
    }


    @GetMapping("/getString")
    public String getValue() {
        Object o = redisTemplate.opsForValue().get(key);
        System.out.println(o);
        return "OK";
    }

    @GetMapping("/hash")
    public String setHash() {
        String key1 = "hash:key";
       /* SysUser user1 = new SysUser(1, "张三", "123456", "男", "上海市嘉定区");
        SysUser user2 = new SysUser(1, "张三", "123456", "男", "上海市嘉定区");
        redisTemplate.opsForHash().put(key1, "user1", user1);
        redisTemplate.opsForHash().put(key1, "user2", user2);*/
        return "OK";
    }

    @GetMapping("/getHash")
    public String getHash() {
        String key1 = "hash:key";
        Object user1 = redisTemplate.opsForHash().get(key1, "user1");
        Map entries = redisTemplate.opsForHash().entries(key1);
        for (Object key : entries.keySet()) {
            System.out.println((String) key + entries.get(key));
        }
        //System.out.println(user1);
        return "OK";
    }


//    @GetMapping("/list")
//    public String setList() {
//       /* SysUser user1 = new SysUser(1, "张三", "123456", "男", "上海市嘉定区");
//        SysUser user2 = new SysUser(1, "李四", "123456", "男", "上海市嘉定区");
//        SysUser user3 = new SysUser(1, "王五", "123456", "男", "上海市嘉定区");*/
//        List<SysUser> list = new ArrayList<>();
//        SysUser user3 = new SysUser();
//        list.add(user3);
//        redisTemplate.opsForList().leftPushAll("list",list);
//
//
//        return "OK";
//    }
//
//    @GetMapping("/getList")
//    public String getList() {
//        List list = redisTemplate.opsForList().range("list", 0, -1);
//        for (Object o : list) {
//            System.out.println(o);
//        }
//        return "OK";
//    }


    @GetMapping("/list1")
    public String getList() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Integer num = i + 1;
            SysUser user = new SysUser();
            user.setUserId(num.longValue());
            user.setUserName("");
            user.setPassword("");
            user.setEmail("");
            user.setMobile("");
            user.setStatus(0);
            user.setCreateBy(0L);
            user.setCreateTime(new Date());
            user.setUpdateBy(0L);
            user.setUpdateTime(new Date());
            user.setShopId(0L);
            user.setIsDelete(0);
            user.setToken("");
            redisTemplate.opsForList().rightPush("list:test", user);
        }
        return "OK";
    }

    @GetMapping("/list2")
    public String getList2() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            int num = i + 1;
            redisTemplate.opsForList().rightPush("list:test", num + "");
        }
        return "OK";
    }

    @GetMapping("/list4")
    public String getList4() {

        redisTemplate.opsForList().rightPush("list:test", "1");
        return "OK";
    }

    @GetMapping("/list3")
    public SysUser getList3() {
        Object o = redisTemplate.opsForList().leftPop("list:test");
        System.out.println(o);
        return (SysUser) o;
    }


    public static void main(String[] args) {
        int rollBackNum = 0;

        try {
            System.out.println("1");
            rollBackNum = 1;
            System.out.println("2");
            rollBackNum = 2;
            int i = 10/0;


        } catch (Exception e) {
            if (rollBackNum == 1) {
                System.out.println("1");
            }
            if (rollBackNum == 2) {
                System.out.println("1");
                System.out.println("2");
            }
        }


    }


}
